package com.lsp.rabbitmqprovider.controller;

import com.lsp.rabbitmqprovider.constans.MQConstants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("TestDirectExchange","TestDirectRouting",map);
        return "ok";
    }

    @GetMapping("/sendTopicMessage")
    public String sendTopicMessage(){

        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "Hello A!";
        rabbitTemplate.convertAndSend("TestTopicExchange","com.order.create",createMessageBody(messageId, messageData));

        String messageId2 = String.valueOf(UUID.randomUUID());
        String messageData2 = "Hello B!";
        rabbitTemplate.convertAndSend("TestTopicExchange","com.sms.create",createMessageBody(messageId2, messageData2));

        String messageId3 = String.valueOf(UUID.randomUUID());
        String messageData3 = "Hello C!";
        rabbitTemplate.convertAndSend("TestTopicExchange","cn.order.create",createMessageBody(messageId3, messageData3));
        return "ok";
    }

    @GetMapping("/sendFanoutMessage")
    public String sendFanoutMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "Fanout, Hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("TestFanoutExchange",null,map);
        return "ok";
    }


    /**
     * 延时队列生产者
     * @param exp
     * @return
     */
    @GetMapping("/sendDelayMessage/{exp}")
    public String sendDelayMessage(@PathVariable("exp") Integer exp){
        String msg = "发送时间：" + LocalDateTime.now();
        rabbitTemplate.convertAndSend(MQConstants.DELAY_EXCHANGE,MQConstants.DELAY_KEY,msg,message -> {
            message.getMessageProperties().setDelay(exp); //单位毫秒
            return message;
        });
        return "---------sendTime"+LocalDateTime.now();
    }

    /**
     * 死信队列生产者
     * @return
     */
    @GetMapping("/sendNackOrDeadLetterMessage")
    public String sendAckOrDelayedMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "AckOrDelayed, Hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("zhengchang_exchange","zhengchang_key",map);
        return "ok";
    }


    private Map<String, Object> createMessageBody(String messageId3, String messageData3) {
        String createTime3 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId3);
        map.put("messageData",messageData3);
        map.put("createTime",createTime3);
        return map;
    }

}
